package com.wdcg.fragment;

import java.util.ArrayList;
import java.util.Random;

import com.wdcg.R;
import com.wdcg.activity.ARGameActivity;
import com.wdcg.activity.MainActivity;
import com.wdcg.activity.PuzzleGameActivity;
import com.wdcg.activity.QuizActivity;
import com.wdcg.activity.listener.OnNavigateToFragmentListener;
import com.wdcg.model.access.DwarfAccess;
import com.wdcg.model.dao.DwarfDescription;
import com.wdcg.view.adapter.DwarfsAdapter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class AchievementsFragment extends ListFragment {
	
	private ArrayList<DwarfDescription> mAchievements;

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
mAchievements = DwarfAccess.getDwarvesDescriptions();
		
		ArrayList<DwarfDescription> lFilteredDwarfs = new ArrayList<DwarfDescription>();
		
		for (DwarfDescription lDwarfDescription : mAchievements) {
			if (lDwarfDescription.getDwarf().getIsWaitingForPlay()
					&& !lDwarfDescription.getDwarf().getIsUnlocked()
					&& !lDwarfDescription.getDwarf().getIsVisible()) {
				lFilteredDwarfs.add(lDwarfDescription);
			}
			else if (!lDwarfDescription.getDwarf().getIsWaitingForPlay()
					&& lDwarfDescription.getDwarf().getIsUnlocked()
					&& !lDwarfDescription.getDwarf().getIsVisible()) {
				lFilteredDwarfs.add(lDwarfDescription);
			}
			
			
			DwarfsAdapter lListAdapter = new DwarfsAdapter(getActivity(), lFilteredDwarfs);
			lListAdapter.setOnItemClickListener(new OnClickListener() {
				
				public void onClick(View aV) {
					Button lButton = (Button) aV;
					//String a = getResources().getString(R.string.achievements_list_button_info);
					if (lButton.getText() == getResources().getString(R.string.achievements_list_button_info)) {
						OnNavigateToFragmentListener lActivity = (OnNavigateToFragmentListener) getActivity();
						if (lActivity != null) {
							DwarfDescriptionFragment lFragment = new DwarfDescriptionFragment();
							Bundle lArgs = new Bundle();
							lArgs.putInt(DwarfDescriptionFragment.PARAM_DWARF_ID, (Integer) lButton.getTag());
							lFragment.setArguments(lArgs);
							lActivity.OnNavigate(lFragment, R.string.dwarf_description_title);
						}
					} else {
						//getActivity().startActivityFromFragment(AchievementsFragment.this, new Intent(), requestCode)
						Random lRandom = new Random();
						Intent lIntent = null;
						switch (lRandom.nextInt(3)) {
						case 0:
							lIntent = new Intent(getActivity(), QuizActivity.class);
							lIntent.putExtra(QuizActivity.PARAM_DWARF_ID, (Integer) lButton.getTag());
							break;
						case 1:
							lIntent = new Intent(getActivity(), PuzzleGameActivity.class);
							lIntent.putExtra(PuzzleGameActivity.PARAM_DWARF_ID, (Integer) lButton.getTag());
							break;
						case 2:
							lIntent = new Intent(getActivity(), ARGameActivity.class);
							lIntent.putExtra(ARGameActivity.PARAM_DWARF_ID, (Integer) lButton.getTag());
							break;
						}
						
						getActivity().startActivity(lIntent);
					}
				}
			});
			setListAdapter(lListAdapter);
		}
	}
}